package suites.utils {
	import org.flexunit.asserts.assertEquals;
	import org.flexunit.asserts.assertFalse;
	import org.flexunit.asserts.assertTrue;
	import org.splink.library.utils.ArrayUtils;

	/**
	 * @author Max Kugland
	 */
	public class ArrayUtilsTest {
		private var _array : Array;
		private var _object : Object;

		[Before]
		public function before() : void {
			_array = new Array;
			_array.push("1");
			_array.push(2);
			_array.push("3");
			_array.push(_object = new Object());
		}

		[After]
		public function after() : void {
			_array = null;			_object = null;
		}

		[Test(description="contains")]
		public function testContains() : void {
			assertTrue(ArrayUtils.contains(_array, "1"));
			assertTrue(ArrayUtils.contains(_array, 2));
			assertTrue(ArrayUtils.contains(_array, "2"));
			assertTrue(ArrayUtils.contains(_array, "3"));
			assertFalse(ArrayUtils.contains(_array, 5));
			assertFalse(ArrayUtils.contains(_array, ""));
			assertTrue(ArrayUtils.contains(_array, _object));
			assertFalse(ArrayUtils.contains(_array, new Object));
		}
		
		[Test(description="remove duplicates")]
		public function testRemoveDuplicates() : void {
			var ar : Array = new Array;
			ar.push("1");
			ar.push("1");			ar.push("3");
			ar.push(2);			ar.push(2);
			var o : Object = new Object;
			ar.push(o);			ar.push(o);
			
			var expected : Array = new Array;
			expected.push("1");
			expected.push("3");			expected.push(2);
			expected.push(o);
			
			var result : Array = ArrayUtils.removeDuplicates(ar);
			
			for (var i : int = 0;i < expected.length;i++) {
				assertEquals(expected[i], result[i]);
			}		}

		[Test(description="remove")]
		public function testRemove() : void {
			assertEquals(ArrayUtils.remove(_array, _object), _object);			assertEquals(ArrayUtils.remove(_array, "3"), "3");			assertEquals(ArrayUtils.remove(_array, "1"), "1");			assertEquals(ArrayUtils.remove(_array, 2), 2);
			
			assertTrue(_array.length == 0);
			assertTrue(true);
		}
		
		[Test(description="shuffle")]
		public function testShuffle() : void {
			var len : uint = _array.length;
			assertEquals(ArrayUtils.shuffle(_array).length, len);
		}

		[Test(description="equals")]
		public function testEquals() : void {
			var a1 : Array = [1,2,3];			var a2 : Array = a1;			assertTrue(ArrayUtils.eq(a1, a2));
			a2 = [1,2,3];
			assertTrue(ArrayUtils.eq(a1, a2));
			a2 = [1,2,4];
			assertFalse(ArrayUtils.eq(a1, a2));
			a2 = [1,2];
			assertFalse(ArrayUtils.eq(a1, a2));
		}
	}
}
